using System;
using System.Collections.Generic;
using System.Text;
using SharpMap.Presentation.Views;
using System.Windows;
using SharpMap.Layers;
using System.Diagnostics;
using SharpMap.Wpf.Rendering;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Input;
namespace SharpMap.Presentation.Wpf
{
    /// <summary>
    /// Provides presentation and editing for single layer
    /// </summary>
    public class LayerStyleView : System.Windows.Controls.Control
    {
        #region Dependency Properties

        #region Layer
        public static readonly DependencyProperty LayerProperty =
            DependencyProperty.Register("Layer", typeof(FeatureLayer),
            typeof(LayerStyleView), 
            new FrameworkPropertyMetadata(LayerPropertyChanged));


        public FeatureLayer Layer
        {
            set
            {
                SetValue(LayerProperty, value);
            }
            get
            {
                return (FeatureLayer)GetValue(LayerProperty);
            }
        }




        static void LayerPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            LayerStyleView This = target as LayerStyleView;
            ILayer iLayer;
            if (This != null 
                && This._listView  != null 
                && (iLayer = e.NewValue as ILayer) != null)
            {
                This._listView.DataContext = This._model  = new LayerStyleViewModel(iLayer, This);
            }
        }
        #endregion

        #endregion
 
        #region Construction
        static LayerStyleView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LayerStyleView), new FrameworkPropertyMetadata(typeof(LayerStyleView)));
            CommandManager.RegisterClassCommandBinding(typeof(LayerStyleView),
                new CommandBinding(LayerCommands.StyleChanged, StyleChangedCommandExecuted));
        }
        #endregion

        static void StyleChangedCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            LayerStyleView This = sender as LayerStyleView;
            
            if (This != null)
            {
                StyleViewEntry ve = e.Parameter as StyleViewEntry;
                This._model.ViewLayerStyleChangeRequested(This, new LayerStyleChangeEventArgs(This.Layer.Style, ve));
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _listView = Template.FindName("Part_StyleItemsList", this) as ListView;

        }

        #region Private Methods
 
  
        #endregion

        #region Fields
        LayerStyleViewModel _model;
        ListView _listView = null;
        #endregion        
    }
}
